<?php
namespace Admin\Controller;
use Common\Controller\BaseController;
abstract class AdminController extends BaseController {
    public function _initialize()
    {
        parent::_initialize();
        if(I('limit'))
            C('LIST_NUMBERS', I('limit')); // 自定分页数
    }

    /**
     * 列表显示
     * @param $listData
     * @param string $tpl
     */
    protected function displayList($listData, $tpl = './Template/Common/list.html') {
        if($listData['toolbarTpl']) {
            $this->assign('toolbarTpl', $listData['toolbarTpl']);
            // 默认事件控制模版
            $events = array(
                'onToolbar'=>'./Template/Common/onToolbar.html',
                'onCheckbox'=>'./Template/Common/onCheckbox.html',
                'onEdit'=>'./Template/Common/onEdit.html',
                'onRow'=>'./Template/Common/onRow.html',
                'onTool'=>'./Template/Common/onTool.html',
                'onSort'=>'./Template/Common/onSort.html',
            );
            foreach ($events as $key=>$event_tpl) {
                if(!is_file($event_tpl))
                    unset($events[$key]);
            }
            $this->assign($events);
            foreach ($listData as $key=>$v) {
                if(preg_match('!^on[A-Z]!', $key)) {
                    $this->assign($key, $v);
                    unset($listData[$key]);
                }
            }
            unset($listData['toolbarTpl']);
        }
        $opt = array(
            'limit'=>C('LIST_NUMBERS')?C('LIST_NUMBERS'):10,
            'id'=>'dataTable',
        );
        $opt = array_merge($opt, $listData);
        $opt['cols'] = array($listData['cols']);
        if(!$tpl) $tpl = './Template/Common/list.html';
        $this->assign('option', $opt);
        $this->display($tpl);
    }


    /**
     * 公共删除页
     * @return [type] [description]
     */
    public function del(){
        if(!$this->model){
            $this->error('没有指定模型');
            return false;
        }
        $model = $this->model;
        $ids    =   I('request.ids')?:I('request.id');
        if(empty($ids)){
            $this->error('请选择要操作的数据');
        }
        $url = $_SERVER['HTTP_REFERER'];
        if($ids){
            /**
             * 自定义前置动作
             * 删除数据前执行操作
             */
            if (method_exists($this, '_beging_del')) {
                $this->_beging_del($ids, $url);
            }
            $map = array('id' => array('in', $ids) );
            $res = $model->where($map)->delete();
            if($res) {
                $this->success('删除成功', $url);
            }else{
                $this->error('删除失败');
            }
        }else{
            $this->error('删除失败');
        }
    }
}